﻿#include "OperSubsystemCavity.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "FITK_Component/FITKAcousticsModel/FITKSEASubSysCavity.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"

#include "OperatorsInterface/GraphEventOperator.h"
#include "updateBrowserTable.h"
#include "GUIWidget/AttributeSetDialog.h"

#include <QMessageBox>

namespace ModelOper
{
    bool OperSubsystemCavity::execGUI()
    {
        return false;
    }
    bool OperSubsystemCavity::execProfession()
    {
        return false;
    }
    void OperSubsystemCavity::editSubsystem(const int seaID)
    {
        if (!_seaMgr)return;
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        //编辑对话框
        QObject object;
        object.setObjectName("actionEditCavity");
        Core::FITKActionOperator* oper = Core::FITKOperatorRepo::getInstance()->getOperatorT<Core::FITKActionOperator>("actionEditCavity");
        if (!oper) return;
        oper->setArgs("DataID", seaID);
        oper->setEmitter(&object);
        oper->actionTriggered();
    }
    void OperSubsystemCavity::removeSubsystem(const int seaID)
    {
        //获取管理器
        if (!_seaMgr)return;
        //移除并更新列表
        _seaMgr->removeDataByID(seaID);
        ModelOper::updateBrowserTable();
        EventOper::GraphEventOperator* operGraph = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("GraphPreprocess");
        operGraph->updateSEASubSystems();
    }
    void OperSubsystemCavity::setAttribute(const QString & name, const QString & text, const int attrIndex, const QList<int>& seaID)
    {
        if (!_seaMgr)return;
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phys == nullptr) return;
        Acoustics::FITKAcousticsMaterialManager* matMgr = phys->getMaterialManager();
        if (matMgr == nullptr) return;
        //设置对话框
        int type = 1;
        if (attrIndex == 1)type = 2;
        GUI::AttributeSetDialog dlg(type, mw);
        dlg.setTextLabel(name);

        if (attrIndex == 1) {
            QList<Acoustics::FITKAcousticsAbsMaterial*>matList = matMgr->getAcousticsMaterials(Acoustics::FITKAcousticsAbsMaterial::Fluid);
            QList<QString> list;
            for (auto value : matList) {
                list += value->getDataObjectName();
            }
            dlg.setDefaultValueQString(list);
        }
        if (attrIndex == 2|| attrIndex == 3) {
            dlg.setDefaultValueDouble(text.toDouble());
        }
        if (dlg.exec() != QDialog::Accepted) return;
        //材料
        QString mat = dlg.getValueQString();
        int matID = -1;
        if (attrIndex == 1) {
            matID = matMgr->getDataByName(mat)->getDataObjectID();
        }
        // 面积或体积
        const double value = dlg.getValueDouble();
        //如果小于等于0，不设置
        if (type==1 && value<1e-64) return;

        for (int id : seaID)
        {
            Acoustics::FITKSEASubSysCavity* cavity = dynamic_cast
                <Acoustics::FITKSEASubSysCavity*>(_seaMgr->getDataByID(id));
            if (cavity == nullptr) continue;
            //不同列设置不同参数
            switch (attrIndex)
            {
            case 1: //材料
                cavity->setMaterialID(matID); break;
            case 2: //面积
                cavity->setSurfaceArea(value); break;
            case 3: //体积
                cavity->setVolume(value); break;
            default: break;
            }
        }
        ModelOper::updateBrowserTable();
    }

}